//
//  DEveryXDaysModel.swift
//  Drop
//
//  Created by hushaohua on 2018/5/28.
//  Copyright © 2018年 init. All rights reserved.
//

import Foundation

enum HTimeIntervalType{
    case day
    case week
    case month
}

class DEveryXDayItem : Equatable{
    static func == (lhs: DEveryXDayItem, rhs: DEveryXDayItem) -> Bool {
        return lhs.day == rhs.day && lhs.title == rhs.title;
    }
    
    var title:String = "";
    var day:Int = 0;
    
    init(_ title:String) {
        self.title = title;
    }
    init(_ title:String, day:Int) {
        self.title = title;
        self.day = day;
    }
}

class DEveryXDaysModel {
    
    var currentDays:[DEveryXDayItem]?;
    func setCurrentWeekDays() -> Void {
        self.currentDays = self.weekDays;
    }
    func setCurrentMonthDays() -> Void {
        self.currentDays = self.monthDays;
    }
    func isCurrentWeekdays() -> Bool{
        return self.currentDays == self.weekDays;
    }
    func isCurrentMonthdays() -> Bool{
        return self.currentDays == self.monthDays;
    }
    lazy var selectedItems:[DEveryXDayItem] = {
        var items = [DEveryXDayItem]();
        return items;
    }();
    
    func selectedDays() -> [Int]{
        var days = [Int]();
        for item in self.selectedItems {
            days.append(item.day);
        }
        return days;
    }
    
    func isItemSelected(_ item:DEveryXDayItem) -> Bool {
        return self.selectedItems.contains(item);
    }
    func selectItem(_ item:DEveryXDayItem){
        self.selectedItems.append(item);
    }
    func selectWeekdayItems() {
        self.selectedItems.removeAll();
        self.selectedItems.append(contentsOf: self.weekDays[0..<5]);
    }
    func selectWeekendItems() {
        self.selectedItems.removeAll();
        self.selectedItems.append(contentsOf: self.weekDays[5..<7]);
    }
    func selectAllWeekdayItems() {
        self.selectedItems.removeAll();
        self.selectedItems.append(contentsOf: self.weekDays);
    }
    
    func UnselectItem(_ item:DEveryXDayItem){
        let index = self.selectedItems.index(of: item);
        if (index != nil){
            self.selectedItems.remove(at: index!);
        }
    }
    func selectedDetail() -> String{
        var string = String();
        for item in self.currentDays! {
            if (self.selectedItems.contains(item)){
                string += item.title;
                string += ","
            }
        }
        if (string.last == ","){
            string.removeLast();
        }
        return  string;
    }
    
    lazy var weekDays:[DEveryXDayItem] = {
        var days = [DEveryXDayItem]();
        days.append(DEveryXDayItem("一", day:2));
        days.append(DEveryXDayItem("二", day:3));
        days.append(DEveryXDayItem("三", day:4));
        days.append(DEveryXDayItem("四", day:5));
        days.append(DEveryXDayItem("五", day:6));
        days.append(DEveryXDayItem("六", day:7));
        days.append(DEveryXDayItem("日", day:1));
        return days;
    }();
    
    lazy var monthDays:[DEveryXDayItem] = {
        var days = [DEveryXDayItem]();
        for index in 1...31 {
            days.append(DEveryXDayItem("\(index)", day:index));
        }
        return days;
    }();
}
